package com.karry.yl.service.services.impl;

import com.karry.commons.base.impl.SuperServiceImpl;
import com.karry.yl.dao.services.ItemsMapper;
import com.karry.yl.dao.services.ServiceHealthDataMapper;
import com.karry.yl.domain.healthdata.HealthDataItem;
import com.karry.yl.domain.services.Items;
import com.karry.yl.domain.services.ServiceHealthData;
import com.karry.yl.service.services.ItemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Service
public class ItemsServiceImpl extends SuperServiceImpl<ItemsMapper,Items> implements ItemsService {
    @Autowired
    private ServiceHealthDataMapper serviceHealthDataMapper;

    @Override
    @Transactional
    public void save(Items item, List<Long> hdItemIds) {
        baseMapper.insert(item);
        if(item.getType() == Items.TYPE_TJ && null != hdItemIds){
            for(Long id : hdItemIds){
                ServiceHealthData serviceHealthData = new ServiceHealthData(item.getId(), id);
                serviceHealthDataMapper.insert(serviceHealthData);
            }
        }
    }

    @Override
    @Transactional
    public void update(Items item, List<Long> hdItemIds) {
        baseMapper.updateById(item);
        serviceHealthDataMapper.deleteByItemId(item.getId());  //删除原来的健康数据项
        if(item.getType() == Items.TYPE_TJ && null != hdItemIds){
            for(Long id : hdItemIds){
                ServiceHealthData serviceHealthData = new ServiceHealthData(item.getId(), id);
                serviceHealthDataMapper.insert(serviceHealthData);
            }
        }
    }

    @Override
    public List<Long> selectHealthDataItem(Long itemId) {
        List<Long> ids = new ArrayList<>();
        List<HealthDataItem> healthDataItems = baseMapper.selectItemList(itemId);
        if(null != healthDataItems){
            for(HealthDataItem hdi :healthDataItems){
                ids.add(hdi.getId());
            }
        }
        return ids;
    }

    @Override
    public List<HealthDataItem> getHealthDataItemsByServiceItemId(Long id) {
        return baseMapper.selectItemList(id);
    }
}
